<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="1上午 2下午 3晚上" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timeQuantum">
              <j-dict-select-tag type="radio" v-model="model.timeQuantum" dictCode="ca_time_quantum" placeholder="请选择1上午 2下午 3晚上" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="第几节" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courseOrder">
              <a-input-number v-model="model.courseOrder" placeholder="请输入第几节" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周一" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="monday">
              <j-switch v-model="model.monday" :options="[1,2]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周二" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tuesday">
              <j-switch v-model="model.tuesday" :options="[1,2]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周三" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wednesday">
              <j-switch v-model="model.wednesday" :options="[1,2]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周四" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="thursday">
              <j-switch v-model="model.thursday" :options="[1,2]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周五" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="friday">
              <j-switch v-model="model.friday" :options="[1,2]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周六" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="saturday">
              <j-switch v-model="model.saturday" :options="[1,2]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周日" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weekday">
              <j-switch v-model="model.weekday" :options="[1,2]" ></j-switch>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CaWorkConfigForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           timeQuantum: [
              { required: true, message: '请输入1上午 2下午 3晚上!'},
           ],
           courseOrder: [
              { required: true, message: '请输入第几节!'},
           ],
           monday: [
              { required: true, message: '请输入周一!'},
           ],
           tuesday: [
              { required: true, message: '请输入周二!'},
           ],
           wednesday: [
              { required: true, message: '请输入周三!'},
           ],
           thursday: [
              { required: true, message: '请输入周四!'},
           ],
           friday: [
              { required: true, message: '请输入周五!'},
           ],
           saturday: [
              { required: true, message: '请输入周六!'},
           ],
           weekday: [
              { required: true, message: '请输入周日!'},
           ],
        },
        url: {
          add: "/ca/caWorkConfig/add",
          edit: "/ca/caWorkConfig/edit",
          queryById: "/ca/caWorkConfig/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>